﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meeting.DBUtility;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Data;
namespace Meeting.BLL
{
    public  class MeetingFiles
    {
        public string GetMeetingFiles(int pageIndex, int pageSize, string filter)
        {
            int nAllRecord = 0, nAllPages = 0;
            DataTable dt = DbHelperSQL.GetPage("Id", " Name,UploadDate,Sort,Comment ", " dbo.MeetingDocument ", filter, " Sort ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            if (dt == null) return "";
            Model.JsonGrid jr = new Model.JsonGrid();
            jr.total = nAllRecord;
            jr.rows = dt;
            return JsonConvert.SerializeObject(jr);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.MeetingDocument model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into MeetingDocument(");
            strSql.Append("MeetingId,TopicId,Sort,Path,Name,Format,UploadDate,Comment,IsActive,ImageCount)");
            strSql.Append(" values (");
            strSql.Append("@MeetingId,@TopicId,@Sort,@Path,@Name,@Format,@UploadDate,@Comment,@IsActive,@ImageCount)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MeetingId", SqlDbType.Int,4),
					new SqlParameter("@TopicId", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Path", SqlDbType.NVarChar,128),
					new SqlParameter("@Name", SqlDbType.NVarChar,256),
					new SqlParameter("@Format", SqlDbType.VarChar,50),
					new SqlParameter("@UploadDate", SqlDbType.DateTime),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1),
					new SqlParameter("@ImageCount", SqlDbType.Int,4)};
            parameters[0].Value = model.MeetingId;
            parameters[1].Value = model.TopicId;
            parameters[2].Value = model.Sort;
            parameters[3].Value = model.Path;
            parameters[4].Value = model.Name;
            parameters[5].Value = model.Format;
            parameters[6].Value = model.UploadDate;
            parameters[7].Value = model.Comment;
            parameters[8].Value = model.IsActive;
            parameters[9].Value = model.ImageCount;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MeetingDocument ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MeetingDocument ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.MeetingDocument GetModel(string name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,MeetingId,TopicId,Sort,Path,Name,Format,UploadDate,Comment,IsActive,ImageCount from MeetingDocument ");
			strSql.Append(" where Name=@Name");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,256)
};
            parameters[0].Value = name;

			Model.MeetingDocument model=new Model.MeetingDocument();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["MeetingId"].ToString()!="")
				{
					model.MeetingId=int.Parse(ds.Tables[0].Rows[0]["MeetingId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TopicId"].ToString()!="")
				{
					model.TopicId=int.Parse(ds.Tables[0].Rows[0]["TopicId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Sort"].ToString()!="")
				{
					model.Sort=int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
				}
				model.Path=ds.Tables[0].Rows[0]["Path"].ToString();
				model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				model.Format=ds.Tables[0].Rows[0]["Format"].ToString();
				if(ds.Tables[0].Rows[0]["UploadDate"].ToString()!="")
				{
					model.UploadDate=DateTime.Parse(ds.Tables[0].Rows[0]["UploadDate"].ToString());
				}
				model.Comment=ds.Tables[0].Rows[0]["Comment"].ToString();
				if(ds.Tables[0].Rows[0]["IsActive"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsActive"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsActive"].ToString().ToLower()=="true"))
					{
						model.IsActive=true;
					}
					else
					{
						model.IsActive=false;
					}
				}
				if(ds.Tables[0].Rows[0]["ImageCount"].ToString()!="")
				{
					model.ImageCount=int.Parse(ds.Tables[0].Rows[0]["ImageCount"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

    }
}
